﻿using Basic.FormCollect;
using Basic.FormModel.Form;
using Basic.FormRemoteModel;
using Basic.FormRemoteModel.QueryInter.Model;
using Basic.UserFormService.Interface;
using WeDonekRpc.Helper;

namespace Basic.UserFormService.Extend
{
    internal class FormRecordService : IFormRecordService
    {
        private readonly IUserFormRecordService _UserForm;
        private readonly IPhyFormService _PhyForm;
        private readonly ICustomFormCollect _Form;
        private readonly IPhyQueryFormService _PhyQueryForm;
        private readonly IFormViewValueService _FormView;
        public FormRecordService ( IUserFormRecordService userForm,
            IPhyFormService phyForm,
            ICustomFormCollect form,
            IFormViewValueService formView,
            IPhyQueryFormService phyQueryForm )
        {
            this._FormView = formView;
            this._UserForm = userForm;
            this._PhyForm = phyForm;
            this._Form = form;
            this._PhyQueryForm = phyQueryForm;
        }

        public Dictionary<long, FormVal> GetValues ( FormValsGetArg arg )
        {
            FormBase form = this._Form.Get<FormBase>(arg.FormId);
            if ( form.FormType == FormType.代理表单 )
            {
                return this._PhyForm.GetValues(form, arg);
            }
            else if ( form.FormType == FormType.物理表单 )
            {
                return this._PhyQueryForm.GetValues(form, arg);
            }
            else if ( form.FormType == FormType.视图 )
            {
                return this._FormView.GetValues(form, arg);
            }
            return this._UserForm.GetValues(form, arg);
        }

        public FormVal GetValue ( FormValGetArg arg )
        {
            FormBase form = this._Form.Get<FormBase>(arg.FormId);
            if ( form.FormType == FormType.代理表单 )
            {
                return this._PhyForm.GetValue(form, arg);
            }
            else if ( form.FormType == FormType.物理表单 )
            {
                return this._PhyQueryForm.GetValue(form, arg);
            }
            else if ( form.FormType == FormType.视图 )
            {
                return this._FormView.GetValue(form, arg);
            }
            return this._UserForm.GetValue(form, arg);
        }

        public Dictionary<long, FormVal> GetVals ( FormGetVals arg )
        {
            long[] formId = arg.Cols.ConvertAll(a => a.FormId);
            FormBase[] form = this._Form.Gets<FormBase>(formId);
            Dictionary<long, FormVal> vals = new Dictionary<long, FormVal>();
            form.ForEach(c =>
            {
                KeyValuePair<long, string>[] cols = arg.Cols.Where(a => a.FormId == c.Id).Select(a => new KeyValuePair<long, string>(a.ColId, a.Keywords)).ToArray();
                if ( c.FormType == FormType.代理表单 )
                {
                    this._PhyForm.LoadValues(c, arg.LinkPK, arg.LinkType, cols, vals);
                }
                else if ( c.FormType == FormType.物理表单 )
                {
                    this._PhyQueryForm.LoadValues(c, arg.LinkPK, arg.LinkType, cols, vals);
                }
                else if ( c.FormType == FormType.视图 )
                {
                    this._PhyQueryForm.LoadValues(c, arg.LinkPK, arg.LinkType, cols, vals);
                }
                else
                {
                    this._UserForm.LoadValues(c, arg.LinkPK, arg.LinkType, cols, vals);
                }
            });
            return vals;
        }
    }
}
